#pragma once
#include "event/event.hpp"

#include <chrono>
#include <functional>
#include <iostream>
#include <mutex>
#include <set>
#include <signal.h>

namespace ibox::event {
void SignalHandlerFunc(int signo, siginfo_t *siginfo, void *context);

using SignalHandler = struct sigaction;
class SignalContext {
public:
    std::set<int> write_fds;   // 通知loop的fd, 每一个loop注册一个
    SignalHandler old_handler; // 原始的信号处理函数
};

class SignalSubscriber {
public:
    void on_signal(int signo) {
        std::cout << "SignalSubscriber::on_signal: " << signo << '\n';
    };
};

class EpollLoop;

class SignalEvent : public Event, public SignalSubscriber {
    using CallbackFunc = std::function<void(int)>;

public:
    SignalEvent(EpollLoop *loop, std::set<int> sigset, Mode mode)
        : m_loop{loop}, m_sigset{std::move(sigset)}, m_mode{mode} {}

    ~SignalEvent() = default;

    void set_callback(CallbackFunc cb) { m_cb = std::move(cb); };
    bool is_enabled() const { return m_is_enabled; };
    bool enable();
    bool disable();

    EpollLoop *get_loop() const { return m_loop; }
    void on_signal(int signo);

private:
    EpollLoop *m_loop{};
    bool m_is_enabled = false;
    std::set<int> m_sigset;
    Mode m_mode = Mode::Oneshot;
    CallbackFunc m_cb{};
};

} // namespace ibox::event
